package com.physics.mps.module.comsol.builder.std;

import com.physics.mps.module.comsol.builder.Builder;
import com.physics.mps.module.comsol.enums.StudyEnum;
import com.physics.mps.module.comsol.vo.StudyVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author mcsoft
 * @date 2023/10/16 15:49
 * @description comsol java 研究求解脚本构建
 */
public class StdBuilder {

    private final Builder builder;

    public StdBuilder(Builder builder) {
        this.builder = builder;
    }

    public void build(StudyVo studyVo, Map<String, String> map) {
        if (studyVo == null) {
            return;
        }

        if (StudyEnum.PSD.equals(studyVo.getStudyEnum())) {
            psdStudyAndSolver(map, studyVo.getParams());
            return;
        }

        String stdTag = builder.uniqueTag("std");
        String stdObj = builder.uniqueTag("study");
        String stdFeatureObj = builder.uniqueTag("studyFeature");

        builder.change(1, 0);
        buildStudy(stdObj, stdTag, stdFeatureObj, studyVo);

        builder.change(1, 0);
        setSolveFor(stdFeatureObj, studyVo.getSolveList());

        builder.change(1, 0);
        buildNormalSolve(studyVo, stdTag, stdFeatureObj);
    }


    /**
     * 随机振动 研究 + 求解
     */
    private void psdStudyAndSolver(Map<String, String> commonMap, Map<String, String> stdMap) {

        // reduced
        String reduced =
                "        model.reduced().create(\"rom1\", \"ModalFrequency\");\n" +
                "        model.reduced().create(\"rvib1\", \"RandomVibration\");\n" +
                "        model.reduced(\"rom1\").set(\"interf\", \"stateless\");\n" +
                "        model.reduced(\"rom1\").set(\"parameters\", new String[]{\"freq\"});\n" +
                "        model.reduced(\"rom1\").set(\"controls\", new String[]{\"accX\", \"accY\", \"accZ\"});\n" +
                "        model.reduced(\"rom1\").set(\"depvarnames\", new String[]{});\n" +
                "        model.reduced(\"rom1\").set(\"depvars\", false);\n" +
                "        model.reduced(\"rom1\").set(\"loadfact\", \"1\");\n" +
                "        model.reduced(\"rvib1\").set(\"frequencyResponseModel\", \"rom1\");\n" +
                "        model.reduced(\"rvib1\").set(\"powerSpectralDensity\", new String[]{\"%s\", \"%s\", \"%s\"});\n" +
                "        model.reduced(\"rvib1\").set(\"fLow\", %s);\n" +
                "        model.reduced(\"rvib1\").set(\"fHigh\", %s);";

        // 振动方向
        String direction = stdMap.getOrDefault("direction", "x");

        switch (direction) {
            case "x": {
                reduced = String.format(reduced, "pw1(freq)", "0", "0", commonMap.getOrDefault("fLow", "20"), commonMap.getOrDefault("fHigh", "2000"));
                break;
            }
            case "y": {
                reduced = String.format(reduced, "0", "pw1(freq)", "0", commonMap.getOrDefault("fLow", "20"), commonMap.getOrDefault("fHigh", "2000"));
                break;
            }
            case "z": {
                reduced = String.format(reduced, "0", "0", "pw1(freq)", commonMap.getOrDefault("fLow", "20"), commonMap.getOrDefault("fHigh", "2000"));
                break;
            }
        }
        builder.change(2, 0);
        builder.append(reduced);



        // ~ 研究1
        String std1Tag = builder.uniqueTag("std");
        psdStudy1(std1Tag, Integer.parseInt(stdMap.getOrDefault("neigs", "12")));

        // ~ 研究2
        String std2Tag = builder.uniqueTag("std");
        psdStudy2(std1Tag, std2Tag);

    }


    /**
     * 随机振动 研究1
     * @param std1Tag
     * @param neigs 所需特征频率数
     */
    private void psdStudy1(String std1Tag, Integer neigs) {
        String studyTag = builder.uniqueTag("study");
        String studyFeatureTag = builder.uniqueTag("studyFeature");
        String solTag = builder.uniqueTag("sol");
        String solverSequenceTag = builder.uniqueTag("solverSequence");
        String solverFeatureTag = builder.uniqueTag("solverFeature");

        String study1 =
                        "        Study %s = model.study().create(\"%s\");\n" +
                        "        StudyFeature studyFeature = %s.create(\"eig\", \"Eigenfrequency\");\n" +
                        "        studyFeature.set(\"useadvanceddisable\", true);\n" +
                        "        studyFeature.set(\"disabledphysics\", new String[]{\"solid/lemm1/d1\"});\n" +
                        "        studyFeature.set(\"neigs\", %d);\n" +
                        "        studyFeature.set(\"neigsactive\", true);\n" +
                        "        studyFeature.set(\"shift\", \"1\");\n"+
                        "        studyFeature.setSolveFor(\"/physics/solid\", true);";
        study1 = String.format(study1, studyTag, std1Tag, studyTag, neigs);
        study1 = study1.replace("studyFeature", studyFeatureTag);

        builder.change(2, 0);
        builder.append(study1);

        String solver1 =
                        "        SolverSequence solverSequence = model.sol().create(\"%s\");\n" +
                        "        solverSequence.study(\"%s\");\n" +
                        "        solverSequence.attach(\"%s\");\n" +
                        "        solverSequence.create(\"st1\", \"StudyStep\");\n" +
                        "        solverSequence.create(\"v1\", \"Variables\");\n" +
                        "        SolverFeature solverFeature = solverSequence.create(\"e1\", \"Eigenvalue\");\n" +
                        "        solverFeature.set(\"transform\", \"eigenfrequency\");\n" +
                        "        solverFeature.set(\"neigs\", %d);\n" +
                        "        solverFeature.set(\"shift\", \"1\");\n" +
                        "        solverFeature.set(\"eigvfunscale\", \"maximum\");\n" +
                        "        solverFeature.set(\"eigvfunscaleparam\", 1.1299999999999999E-7);\n" +
                        "        solverFeature.feature(\"dDef\").label(\"\\u76f4\\u63a5 1\");\n" +
                        "        solverFeature.feature(\"aDef\").label(\"\\u9ad8\\u7ea7 1\");\n" +
                        "        solverFeature.feature(\"aDef\").set(\"cachepattern\", true);\n" +
                        "        solverSequence.runAll();";
        solver1 = String.format(solver1, solTag, std1Tag, std1Tag, neigs);
        solver1 = solver1.replace("solverSequence", solverSequenceTag);
        solver1 = solver1.replace("solverFeature", solverFeatureTag);

        builder.change(2, 0);
        builder.append(solver1);
    }


    /**
     * 随机振动 研究2
     * @param std1Tag
     * @param std2Tag
     */
    private void psdStudy2(String std1Tag, String std2Tag) {
        String studyTag = builder.uniqueTag("study");
        String studyFeatureTag = builder.uniqueTag("studyFeature");
        String solTag = builder.uniqueTag("sol");
        String solverSequenceTag = builder.uniqueTag("solverSequence");

        String study2 =
                "        Study %s = model.study().create(\"%s\");\n" +
                "        StudyFeature studyFeature = %s.create(\"mr\", \"ModelReduction\");\n" +
                "        studyFeature.feature().create(\"freq1\", \"Frequency\");\n" +
                "        studyFeature.set(\"trainingStudy\", \"%s\");\n" +
                "        studyFeature.set(\"trainingStep\", \"eig\");\n" +
                "        studyFeature.set(\"trainingStepConstr\", \"auto\");\n" +
                "        studyFeature.set(\"unreducedModelStudy\", \"%s\");\n" +
                "        studyFeature.set(\"unreducedModelStep\", \"freq1\");\n" +
                "        studyFeature.set(\"romdata\", \"rom1\");\n" +
                "        studyFeature.feature(\"freq1\").setIndex(\"plist\", \"100\", 0);\n"+
                "        studyFeature.setSolveFor(\"/physics/solid\", true);";
        study2 = String.format(study2, studyTag, std2Tag, studyTag, std1Tag, std2Tag);
        study2 = study2.replace("studyFeature", studyFeatureTag);

        builder.change(2, 0);
        builder.append(study2);

        String solver2 =
                "        SolverSequence solverSequence = model.sol().create(\"%s\");\n" +
                "        solverSequence.study(\"%s\");\n" +
                "        solverSequence.attach(\"%s\");\n" +
                "        solverSequence.create(\"st1\", \"StudyStep\");\n" +
                "        solverSequence.create(\"v1\", \"Variables\");\n" +
                "        solverSequence.create(\"mor1\", \"ModalReduction\");\n" +
                "        solverSequence.create(\"st2\", \"StudyStep\");\n" +
                "        solverSequence.feature(\"v1\").set(\"clist\", new int[]{100});\n" +
                "        solverSequence.feature(\"mor1\").set(\"plist\", 100);\n" +
                "        solverSequence.feature(\"mor1\").set(\"linearity\", false);\n" +
                "        solverSequence.feature(\"mor1\").set(\"eigsol\", \"sol1\");\n" +
                "        solverSequence.feature(\"mor1\").set(\"romdata\", \"rom1\");\n" +
                "        solverSequence.feature(\"mor1\").feature(\"dDef\").label(\"\\u76f4\\u63a5 1\");\n" +
                "        solverSequence.feature(\"mor1\").feature(\"aDef\").label(\"\\u9ad8\\u7ea7 1\");\n" +
                "        solverSequence.feature(\"mor1\").feature(\"aDef\").set(\"cachepattern\", true);\n" +
                "        solverSequence.feature(\"st2\").set(\"useForModelReduction\", false);\n" +
                "        solverSequence.runAll();";
        solver2 = String.format(solver2, solTag, std2Tag, std2Tag);
        solver2 = solver2.replace("solverSequence", solverSequenceTag);

        builder.change(2, 0);
        builder.append(solver2);
    }



    /**
     * buildStudy
     * @param stdObj
     * @param stdTag
     * @param stdFeatureObj
     * @param studyVo
     */
    private void buildStudy(String stdObj, String stdTag, String stdFeatureObj, StudyVo studyVo) {
        StudyEnum studyEnum = studyVo.getStudyEnum();

        builder.change(1, 2);
        builder.append("Study ");
        builder.append(stdObj);
        builder.append(" = model.study().create(");
        builder.value(stdTag);
        builder.append(");");

        builder.change(1, 2);
        builder.append("StudyFeature ");
        builder.append(stdFeatureObj);
        builder.append(" = ");
        builder.append(stdObj);
        builder.append(".create(");
        builder.value(studyEnum.getTag());
        builder.append(", ");
        builder.value(studyEnum.getType());
        builder.append(");");

        // 瞬态并且设置了输出步长
        if (Objects.equals(studyEnum, StudyEnum.Transient) && !StringUtils.isEmpty(studyVo.getTList())) {
            builder.change(1, 2);
            builder.append(stdFeatureObj);
            builder.append(".set(\"tlist\", ");
            builder.append("\"range(0,0.1,");
            builder.append(studyVo.getTList());
            builder.append(")\");");
        }
    }


    /**
     * setSolveFor
     * @param stdFeatureObj
     * @param solveList
     */
    private void setSolveFor(String stdFeatureObj, List<String> solveList) {
        if (CollectionUtils.isEmpty(solveList)) {
            return;
        }
        solveList.forEach(uri -> {
            builder.change(1, 2);
            builder.append(stdFeatureObj);
            builder.append(".setSolveFor(");
            builder.value(uri);
            builder.append(", true);");
        });
    }


    /**
     * 瞬态求解配置
     * @param studyVo
     * @param stdTag
     * @param stdFeatureObj
     */
    private void buildNormalSolve(StudyVo studyVo, String stdTag, String stdFeatureObj) {

        String solTag = builder.uniqueTag("sol");
        String solObj = builder.uniqueTag("solution");

        builder.change(1, 2);
        builder.append("SolverSequence ");
        builder.append(solObj);
        builder.append(" = model.sol().create(");
        builder.value(solTag);
        builder.append(");");

        builder.change(1, 2);
        builder.append(solObj);
        builder.append(".study(");
        builder.value(stdTag);
        builder.append(");");


        // 瞬态
        builder.change(2, 2);
        builder.append(stdFeatureObj);
        builder.append(".set(\"notlistsolnum\", 1);");

        builder.change(1, 2);
        builder.append(stdFeatureObj);
        builder.append(".set(\"notsolnum\", \"auto\");");

        builder.change(1, 2);
        builder.append(stdFeatureObj);
        builder.append(".set(\"listsolnum\", 1);");

        builder.change(1, 2);
        builder.append(stdFeatureObj);
        builder.append(".set(\"solnum\", \"auto\");");

        builder.change(2, 0);
        switch (studyVo.getSolveEnum()) {
            case HeatTransferInSolids: {
                solHeatTransferInSolids(studyVo.getTList());
                break;
            }
            case ThermalStructureInteraction: {
                solThermalStructureInteraction(studyVo.getTList());
                break;
            }
            case HeatTransferInSolidsAndFluids: {
                solHeatTransferInSolidsAndFluids(studyVo.getTList());
                break;
            }
        }
    }


    /**
     * 固体传热
     * @param tList
     */
    private void solHeatTransferInSolids(String tList) {
        builder.append(
                "    model.sol(\"sol1\").create(\"st1\", \"StudyStep\");\n" +
                "    model.sol(\"sol1\").feature(\"st1\").set(\"study\", \"std1\");\n" +
                "    model.sol(\"sol1\").feature(\"st1\").set(\"studystep\", \"time\");\n" +
                "    model.sol(\"sol1\").create(\"v1\", \"Variables\");\n" +
                "    model.sol(\"sol1\").feature(\"v1\").set(\"control\", \"time\");\n" +
                "    model.sol(\"sol1\").create(\"t1\", \"Time\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"tlist\", \"range(0,0.1," + tList + ")\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"plot\", \"off\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"plotgroup\", \"Default\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"plotfreq\", \"tout\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"probesel\", \"all\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"probes\", new String[]{});\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"probefreq\", \"tsteps\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"atolglobalvaluemethod\", \"factor\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"atolmethod\", new String[]{\"comp1_T\", \"global\"});\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"atol\", new String[]{\"comp1_T\", \"1e-3\"});\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"atolvaluemethod\", new String[]{\"comp1_T\", \"factor\"});\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"endtimeinterpolation\", true);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"estrat\", \"exclude\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"maxorder\", 2);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").set(\"control\", \"time\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").create(\"fc1\", \"FullyCoupled\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"jtech\", \"once\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"damp\", 0.9);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"stabacc\", \"aacc\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"aaccdim\", 5);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"aaccmix\", 0.9);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"aaccdelay\", 1);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").create(\"d1\", \"Direct\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"d1\").set(\"linsolver\", \"pardiso\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"d1\").set(\"pivotperturb\", 1.0E-13);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"d1\").label(\"\\u76f4\\u63a5\\uff0c\\u4f20\\u70ed\\u53d8\\u91cf (ht)\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").create(\"i1\", \"Iterative\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"linsolver\", \"gmres\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"prefuntype\", \"left\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"itrestart\", 50);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"rhob\", 20);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"maxlinit\", 10000);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"nlinnormuse\", \"on\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").label(\"AMG\\uff0c\\u4f20\\u70ed\\u53d8\\u91cf (ht)\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").create(\"mg1\", \"Multigrid\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"prefun\", \"saamg\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"mgcycle\", \"v\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"maxcoarsedof\", 50000);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"strconn\", 0.01);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"nullspace\", \"constant\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"usesmooth\", false);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"saamgcompwise\", true);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"loweramg\", true);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"pr\").create(\"so1\", \"SOR\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"pr\").feature(\"so1\").set(\"iter\", 2);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"pr\").feature(\"so1\").set(\"relax\", 0.9);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"po\").create(\"so1\", \"SOR\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"po\").feature(\"so1\").set(\"iter\", 2);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"po\").feature(\"so1\").set(\"relax\", 0.9);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"cs\").create(\"d1\", \"Direct\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"cs\").feature(\"d1\")\n" +
                "         .set(\"linsolver\", \"pardiso\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"cs\").feature(\"d1\")\n" +
                "         .set(\"pivotperturb\", 1.0E-13);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"linsolver\", \"d1\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"jtech\", \"once\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"damp\", 0.9);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"stabacc\", \"aacc\");\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"aaccdim\", 5);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"aaccmix\", 0.9);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"aaccdelay\", 1);\n" +
                "    model.sol(\"sol1\").feature(\"t1\").feature().remove(\"fcDef\");\n" +
                "    model.sol(\"sol1\").attach(\"std1\");\n" +
                "    model.sol(\"sol1\").runAll();"
        );
    }


    /**
     * 热固耦合
     * @param tList
     */
    private void solThermalStructureInteraction(String tList) {
        builder.append("" +
                "        model.sol(\"sol1\").create(\"st1\", \"StudyStep\");\n" +
                "        model.sol(\"sol1\").feature(\"st1\").set(\"study\", \"std1\");\n" +
                "        model.sol(\"sol1\").feature(\"st1\").set(\"studystep\", \"time\");\n" +
                "        model.sol(\"sol1\").create(\"v1\", \"Variables\");\n" +
                "        model.sol(\"sol1\").feature(\"v1\").feature(\"comp1_u\").set(\"scalemethod\", \"manual\");\n" +
                "        model.sol(\"sol1\").feature(\"v1\").feature(\"comp1_u\").set(\"scaleval\", \"1e-2*0.11256389330064948\");\n" +
                "        model.sol(\"sol1\").feature(\"v1\").set(\"control\", \"time\");\n" +
                "        model.sol(\"sol1\").create(\"t1\", \"Time\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"tlist\", \"range(0,0.1," + tList + ")\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"plot\", \"off\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"plotgroup\", \"Default\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"plotfreq\", \"tout\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"probesel\", \"all\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"probes\", new String[]{});\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"probefreq\", \"tsteps\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"rtol\", 0.001);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"atolglobalvaluemethod\", \"factor\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"atolmethod\", new String[]{\"comp1_T\", \"global\", \"comp1_u\", \"global\"});\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"atol\", new String[]{\"comp1_T\", \"1e-3\", \"comp1_u\", \"1e-3\"});\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"atolvaluemethod\", new String[]{\"comp1_T\", \"factor\", \"comp1_u\", \"factor\"});\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"endtimeinterpolation\", true);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"estrat\", \"exclude\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"maxorder\", 2);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"control\", \"time\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"aDef\").set(\"cachepattern\", true);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").create(\"se1\", \"Segregated\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").feature().remove(\"ssDef\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").create(\"ss1\", \"SegregatedStep\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").feature(\"ss1\").set(\"segvar\", new String[]{\"comp1_T\"});\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").feature(\"ss1\").set(\"subdamp\", 1);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").feature(\"ss1\").set(\"subjtech\", \"once\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").create(\"d1\", \"Direct\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"d1\").set(\"linsolver\", \"pardiso\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"d1\").set(\"pivotperturb\", 1.0E-13);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"d1\").label(\"\\u76f4\\u63a5\\uff0c\\u4f20\\u70ed\\u53d8\\u91cf ht (te1)\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").feature(\"ss1\").set(\"linsolver\", \"d1\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").feature(\"ss1\").label(\"\\u6e29\\u5ea6\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").create(\"ss2\", \"SegregatedStep\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").feature(\"ss2\").set(\"segvar\", new String[]{\"comp1_u\"});\n" +
                "        model.sol(\"sol1\").feature(\"t1\").create(\"d2\", \"Direct\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"d2\").set(\"linsolver\", \"pardiso\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"d2\").set(\"pivotperturb\", 1.0E-9);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"d2\")\n" +
                "         .label(\"\\u5efa\\u8bae\\u7684\\u76f4\\u63a5\\u6c42\\u89e3\\u5668 solid (te1)\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").feature(\"ss2\").set(\"linsolver\", \"d2\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").feature(\"ss2\").label(\"\\u56fa\\u4f53\\u529b\\u5b66\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").set(\"segstabacc\", \"segaacc\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").set(\"segaaccdim\", 5);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").set(\"segaaccmix\", 0.9);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").set(\"segaaccdelay\", 0);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").create(\"ll1\", \"LowerLimit\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"se1\").feature(\"ll1\").set(\"lowerlimit\", \"comp1.T 0 \");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").create(\"i1\", \"Iterative\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"linsolver\", \"gmres\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"rhob\", 400);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"nlinnormuse\", true);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\")\n" +
                "         .label(\"\\u5efa\\u8bae\\u7684\\u8fed\\u4ee3\\u6c42\\u89e3\\u5668 solid (te1)\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").create(\"mg1\", \"Multigrid\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"prefun\", \"gmg\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"pr\").create(\"so1\", \"SOR\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"pr\").feature(\"so1\").set(\"relax\", 0.8);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"po\").create(\"so1\", \"SOR\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"po\").feature(\"so1\").set(\"relax\", 0.8);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"cs\").create(\"d1\", \"Direct\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"cs\").feature(\"d1\")\n" +
                "         .set(\"linsolver\", \"pardiso\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"cs\").feature(\"d1\")\n" +
                "         .set(\"pivotperturb\", 1.0E-9);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").create(\"i2\", \"Iterative\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").set(\"linsolver\", \"gmres\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").set(\"prefuntype\", \"left\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").set(\"itrestart\", 50);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").set(\"rhob\", 20);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").set(\"maxlinit\", 10000);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").set(\"nlinnormuse\", \"on\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").label(\"AMG\\uff0c\\u4f20\\u70ed\\u53d8\\u91cf ht (te1)\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").create(\"mg1\", \"Multigrid\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").set(\"prefun\", \"saamg\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").set(\"mgcycle\", \"v\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").set(\"maxcoarsedof\", 50000);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").set(\"strconn\", 0.01);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").set(\"nullspace\", \"constant\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").set(\"usesmooth\", false);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").set(\"saamgcompwise\", true);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").set(\"loweramg\", true);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").feature(\"pr\").create(\"so1\", \"SOR\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").feature(\"pr\").feature(\"so1\").set(\"iter\", 2);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").feature(\"pr\").feature(\"so1\").set(\"relax\", 0.9);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").feature(\"po\").create(\"so1\", \"SOR\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").feature(\"po\").feature(\"so1\").set(\"iter\", 2);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").feature(\"po\").feature(\"so1\").set(\"relax\", 0.9);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").feature(\"cs\").create(\"d1\", \"Direct\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").feature(\"cs\").feature(\"d1\")\n" +
                "         .set(\"linsolver\", \"pardiso\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i2\").feature(\"mg1\").feature(\"cs\").feature(\"d1\")\n" +
                "         .set(\"pivotperturb\", 1.0E-13);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature().remove(\"fcDef\");\n" +
                "        model.sol(\"sol1\").attach(\"std1\");\n" +
                "        model.sol(\"sol1\").runAll();");
    }


    /**
     * 固体和流体传热
     * @param tList
     */
    private void solHeatTransferInSolidsAndFluids(String tList) {
        builder.append(
                "        model.sol(\"sol1\").create(\"st1\", \"StudyStep\");\n" +
                "        model.sol(\"sol1\").feature(\"st1\").set(\"study\", \"std1\");\n" +
                "        model.sol(\"sol1\").feature(\"st1\").set(\"studystep\", \"time\");\n" +
                "        model.sol(\"sol1\").create(\"v1\", \"Variables\");\n" +
                "        model.sol(\"sol1\").feature(\"v1\").set(\"control\", \"time\");\n" +
                "        model.sol(\"sol1\").create(\"t1\", \"Time\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"tlist\", \"range(0,0.1," + tList + ")\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"plot\", \"off\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"plotgroup\", \"Default\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"plotfreq\", \"tout\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"probesel\", \"all\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"probes\", new String[]{});\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"probefreq\", \"tsteps\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"atolglobalvaluemethod\", \"factor\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"atolmethod\", new String[]{\"comp1_T\", \"global\"});\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"atol\", new String[]{\"comp1_T\", \"1e-3\"});\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"atolvaluemethod\", new String[]{\"comp1_T\", \"factor\"});\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"endtimeinterpolation\", true);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"estrat\", \"exclude\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"maxorder\", 2);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").set(\"control\", \"time\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").create(\"fc1\", \"FullyCoupled\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"jtech\", \"once\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"damp\", 0.9);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"stabacc\", \"aacc\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"aaccdim\", 5);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"aaccmix\", 0.9);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"aaccdelay\", 1);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").create(\"d1\", \"Direct\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"d1\").set(\"linsolver\", \"pardiso\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"d1\").set(\"pivotperturb\", 1.0E-13);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"d1\").label(\"\\u76f4\\u63a5\\uff0c\\u4f20\\u70ed\\u53d8\\u91cf (ht)\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").create(\"i1\", \"Iterative\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"linsolver\", \"gmres\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"prefuntype\", \"left\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"itrestart\", 50);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"rhob\", 20);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"maxlinit\", 10000);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").set(\"nlinnormuse\", \"on\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").label(\"AMG\\uff0c\\u4f20\\u70ed\\u53d8\\u91cf (ht)\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").create(\"mg1\", \"Multigrid\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"prefun\", \"saamg\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"mgcycle\", \"v\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"maxcoarsedof\", 50000);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"strconn\", 0.01);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"nullspace\", \"constant\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"usesmooth\", false);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"saamgcompwise\", true);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").set(\"loweramg\", true);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"pr\").create(\"so1\", \"SOR\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"pr\").feature(\"so1\").set(\"iter\", 2);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"pr\").feature(\"so1\").set(\"relax\", 0.9);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"po\").create(\"so1\", \"SOR\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"po\").feature(\"so1\").set(\"iter\", 2);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"po\").feature(\"so1\").set(\"relax\", 0.9);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"cs\").create(\"d1\", \"Direct\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"cs\").feature(\"d1\")\n" +
                "         .set(\"linsolver\", \"pardiso\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"i1\").feature(\"mg1\").feature(\"cs\").feature(\"d1\")\n" +
                "         .set(\"pivotperturb\", 1.0E-13);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"linsolver\", \"d1\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"jtech\", \"once\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"damp\", 0.9);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"stabacc\", \"aacc\");\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"aaccdim\", 5);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"aaccmix\", 0.9);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature(\"fc1\").set(\"aaccdelay\", 1);\n" +
                "        model.sol(\"sol1\").feature(\"t1\").feature().remove(\"fcDef\");\n" +
                "        model.sol(\"sol1\").attach(\"std1\");\n" +
                "        model.sol(\"sol1\").runAll();"
        );
    }




}
